<?php

namespace Modules\Car\Http\Controllers\Api;

use App\Helpers\Constant;
use App\Helpers\NewBaseData;
use App\Helpers\PayTrait;
use App\Helpers\ResponseData;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Car\Entities\CarOrder;
use Modules\Car\Entities\RecognizeDrivingLicense;
use App\Http\Controllers\Miniprograme\SubscribeController;
use App\Models\Car\CarUsedDate;

class UserController extends Controller
{
    use NewBaseData,ResponseData,PayTrait;
    /**
     * 用户搜索
     * @param Request $request
     * @return mixed
     */
   public function update(Request $request){
       if($request->filled('openid')){
           $account = User::query()->updateOrCreate(['openid'=>$request->openid],$request->all());
           return $this->success('更新成功');
       }else{
           return $this->failed('openid不能为空',201);
       }
   }
   //更新小程序时间
   public function updateDate(Request $request){
       User::query()->where('openid',$request->openid)->update(['updated_at'=>now()]);
       return $this->success('更新成功');
   }
   //获取待支付订单列表
   public function order(Request $request){
       $status = $request->input('status','');
       $size = $request->input('size',10);
       $data = CarOrder::query()
           ->with('user','contracts')
           ->when($status,function($query)use($status){
               $query->where('status',$status);
           })
           ->where('openid',$request->openid)
           ->orWhere('openid_customer',$request->openid)
           ->orderByDesc('id')
           ->paginate($size);
       return $this->success($data);
   }
   //获取订单详情
    public function orderDetail(Request $request){
        $status = $request->input('status','');
        $data = CarOrder::query()
            ->with(['user','car','changeCar', 'contracts', 'gear'])
            ->when($status,function($query)use($status){
                $query->where('status',$status);
            })
            ->where(function($query)use($request){
                return $query->where('openid',$request->openid)->orWhere('openid_customer',$request->openid);
            })
            // ->where('openid',$request->openid)
            // ->orWhere('openid_customer',$request->openid)
            ->where('order_sn',$request->order_sn)
            ->first();
        return $this->success($data);
    }
   //获取签署订单详情
    public function orderDetailSign(Request $request){
        $data = CarOrder::query()
            ->with(['user','car','changeCar', 'contracts', 'gear'])
            ->where('order_sn',$request->order_sn)
            ->first();
        return $this->success($data);
    }
    /**
     * 在线支付
     * @param Request $request
     * @return mixed
     */
   public function pay(Request $request){
       $order = CarOrder::query()
            ->where(function($query)use($request){
                return $query->where('openid',$request->openid)->orWhere('openid_customer',$request->openid);
            })
            // ->where('openid',$request->openid)
            // ->orWhere('openid_customer',$request->openid)
           ->where('order_sn',$request->order_sn)
           ->where('status',0)
           ->first();
       if(!$order){
           return $this->failed('订单不存在',201);
       }else{
           $result = $this->miniv2Data($order);
           return $this->success($result);
       }
   }
   //获取空闲车辆
   public function idleCar(Request $request){
        $firstdate = new \DateTime($request->pre_fetch_time); // 租期开始时间
        $lastdate = new \DateTime($request->pre_return_time); // 租期结束时间
        $firstyear = $firstdate->format("Y");
        $firstmonth = $firstdate->format("m");
        $firstday = $firstdate->format("d");
        $lastyear = $lastdate->format("Y");
        $lastmonth = $lastdate->format("m");
        $lastday = $lastdate->format("d");
        $firsttime = (int)($firstyear.$firstmonth.$firstday);
        $lasttime = (int)($lastyear.$lastmonth.$lastday);
        $datelist = CarUsedDate::query()->where('car_id',$request->car_series_id)->where('time','>=',$firsttime)->where('time','<=',$lasttime)->where('is_used', 2)->pluck('car_id'); // 拿到所有已用ID
        $datelist = $datelist->unique(); // 去重后已租空的id
        if(count($datelist)==0){
            return $this->success(1);
        }else{
            return $this->success(0);
        }
    //    $car_series_id = $request->car_series_id;
    //    $count = RecognizeDrivingLicense::query()
    //        ->where('car_series_id',$car_series_id)
    //        ->where('is_used',Constant::CAR_EMPTY)
    //         ->where('status',Constant::CAR_USE_STATUS_EMPTY)
    //        ->count();
    //    if($count){
    //        return $this->success($count);
    //    }else{
    //        return $this->success(0);
    //    }
   }

   //获取订单详情
   public function sendtemptest(Request $request){
        $data = CarOrder::query()
            ->where('order_sn',$request->order_sn)
            ->first();
        if ($data->openid) {
            if ($request->type==1) {
                // 模板1
                (new SubscribeController())->create_order($data);
            } else if ($request->type==2) {
                // 模板2
                (new SubscribeController())->send_arrive($data, '测试发送');
            } else if ($request->type==3) {
                // 模板3
                (new SubscribeController())->manager($data->openid,$data);
            }
        }
        return $this->success($data);
    }




}
